// import { PAGE_SIZE } from '../config/Config'

export default {
  methods: {
    reset (to) {
      if (WXEnvironment.platform === 'Web') {
        if (this.$router) {
          this.$router.replace(to)
        }
      } else {
        if (this.$router) {
          this.$router.replace(to)
        }
      }
    },
    // getModel(){
    //     return weex.requireModule('modal');
    // },
    getNavigator () {
      return weex.requireModule('navigator')
    },
    toBack () {
      if (WXEnvironment.platform === 'Web') {
        this.$router.back()
      } else {
        this.getNavigator().pop({animated: 'true'})
      }
    },
    getBaseUrl (url) {
      // eslint-disable-next-line no-new-wrappers
      let bundleUrl = new String(weex.config.bundleUrl)
      console.log('bundleUrl: ', bundleUrl)
      const isAndroidAssets = bundleUrl.indexOf('file://assets/') >= 0
      const isIOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('sign-app') > 0
      let nativeBase
      if (isAndroidAssets) {
        nativeBase = 'file://assets/dist/' + url
      } else if (isIOSAssets) {
        nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1) + url
      } else {
        let host = 'localhost:8081'
        // eslint-disable-next-line no-useless-escape
        const matches = /\/\/([^\/]+?)\//.exec(bundleUrl)
        if (matches && matches.length >= 2) {
          host = matches[1]
        }

        if (typeof window === 'object') {
          nativeBase = 'http://' + host + '/'
        } else {
          nativeBase = 'http://' + host + '/'
        }
        nativeBase = nativeBase + url + '.html'
      }
      return nativeBase
    },
    jumpInter (to) {
      if (this.$router) {
        this.$router.push(to)
      }
    },
    // object 转 URL 参数
    createQuery (obj) {
      if (obj === null || obj === '' || obj.length === 0) {
        return ''
      }
      let url = '?'
      for (let key in obj) {
        if (obj[key] !== null) {
          url += (key + '=' + encodeURIComponent(obj[key]) + '&')
        }
      }
      return url.substring(0, url.lastIndexOf('&'))
    },
    // 'xxx.js?name=aa' 转 {name: 'aa'}
    // http://192.168.1.47:8080/RepositoryDetailPage.js?userName=DeftMKJ&reposName=MKJWechat&title=MKJWechat
    getQueryData (url) {
      url = url.substring(url.indexOf('.js?') + 3)
      let result = {}
      if (url.indexOf('?') !== -1) {
        let str = url.substr(1)
        let strs = str.split('&')
        for (let i = 0; i < strs.length; i++) {
          result[strs[i].split('=')[0]] = decodeURIComponent(strs[i].split('=')[1])
        }
      }
      return result
    },
    /*
        declare type WeexConfigAPI = {
            bundleUrl: string;
            bundleType?: string;
            env: WeexEnvironment;
        }
         */
    getQuery () {
      if (WXEnvironment.platform === 'Web') {
        return this.$route.params
      } else {
        return this.getQueryData(weex.config.bundleUrl)
      }
    },
    getUserInfo () {
      return this.$store.state.user.userInfo
    },
    getPageSize () {
      // return PAGE_SIZE
    }
  }
}
